package com.kzd.gtgame.main.access

import android.text.Editable
import android.view.inputmethod.EditorInfo
import androidx.core.widget.addTextChangedListener
import com.kzd.gtgame.R
import com.kzd.gtgame.app.SPSConstance
import com.kzd.gtgame.custom.setOnClickAntiShakeListener
import com.kzd.gtgame.jetpack.vm.AccessViewModel
import com.kzd.gtgame.main.AccessAction
import com.module.lemlin.owner.OwnerViewModeFragment
import kotlinx.android.synthetic.main.fragment_access_setting.*

class AccessSettingFragment : OwnerViewModeFragment<AccessViewModel>() {

    companion object {
        fun newInstance(): AccessSettingFragment {
            return AccessSettingFragment()
        }
    }

    override fun viewModel(): AccessViewModel {
        return createViewModel(requireActivity())
    }

    override val layoutResId: Int
        get() = R.layout.fragment_access_setting

    override fun initView() {

        fillData(viewModel.mAccessAction)

        etAccessSettingContent.addTextChangedListener {
            tvAccessSettingSubmit.isEnabled = !it.isNullOrEmpty()
        }
        tvAccessSettingSubmit.setOnClickAntiShakeListener {
            if (viewModel.mAccessAction == AccessAction.SETTING_QQ) {
                viewModel.userChange(qq = etAccessSettingContent.text.toString())
            } else {
                viewModel.userChange(pet_name = etAccessSettingContent.text.toString())
            }
        }
    }

    override fun initViewMode() {

    }

    private fun fillData(action: AccessAction) {

        tvAccessSettingLabel.text = if (action == AccessAction.SETTING_QQ)
            "Q\tQ" else "昵\t称"

        etAccessSettingContent.inputType = if (action == AccessAction.SETTING_QQ)
            EditorInfo.TYPE_CLASS_NUMBER else EditorInfo.TYPE_CLASS_TEXT

        etAccessSettingContent.hint = if (action == AccessAction.SETTING_QQ)
            "请填写QQ" else "请填写昵称"

        etAccessSettingContent.text = if (action == AccessAction.SETTING_QQ) {
            Editable.Factory.getInstance().newEditable(SPSConstance.userInfo?.qq)
        } else {
            Editable.Factory.getInstance().newEditable(SPSConstance.userInfo?.pet_name)
        }
    }

}